<?php

namespace App\Models;

use App\Constants\AssetLogType;
use App\Services\Base\CommonService;
use App\Services\Base\HelpService;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Support\Facades\Cache;

/**
 * This is the model class for table "asset_logs"
 *
 * @property string $id
 * @property string $uid 用户ID
 * @property string $sn sn
 * @property string $rid 推荐用户
 * @property string $symbol 币种标识
 * @property string $wallet_id 钱包ID
 * @property string $type 业务类型
 * @property string $amount 金额
 * @property string $to_amount 兑换金额
 * @property string $balance 帐变后余额
 * @property string $related_sn 关联业务单号
 * @property string $remark 备注
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 */
class AssetLog extends BaseModel
{
    use HasDateTimeFormatter;

    protected $table = 'asset_logs';
    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'id');
    }

    public function log($uid,
                        $amount,
                        $balance,
                        $type,
                        $related_sn,
                        $symbol,
                        $balance_type,
                        $rid = 0
    )
    {
        if ($amount == 0) {
            return false;
        }

        $type_string = tt_asset($type) ?? "";
        if (!$type_string) {
            $this->error(2002);
        }

        $symbol = "USDT";
        $return = $this->getModel()->newQuery()->create(
            [
                'uid' => $uid,
                'symbol' => $symbol,
                'type' => $type,
                'amount' => $amount,
                'balance' => $balance,
                'related_sn' => $related_sn,
                'remark' => $type_string . ":{$amount} {$symbol}",
                'balance_type' => $balance_type,
                'rid' => $rid
            ]
        );

        if (!$return) {
            $this->error(405);
        } else {
            return $return->id;
        }
    }

    public function getList($data)
    {
        try {
            $model = $this->getModel()->newQuery()->where('uid', $data['uid']);
            $service = new CommonService();
            $result = $service->getListById($model, $data, ["id", "uid", "rid", "symbol", "type", "amount", "created_at"]);
            foreach ($result as $k => &$v) {
                $v['user'] = $this->getNameAvatar($v['uid']);
                $v['type_text'] = tt_asset($v['type']);
                $v['amount'] = apiNewMoney($v['amount']);
                unset($v['uid']);
                unset($v['rid']);
            }
            return $result;
        } catch (\Exception $e) {
            HelpService::log($e);
            $this->error($e->getCode(), $e->getMessage());
        }
    }

    public function detail($data)
    {

    }


    public static function getRebateSum($uid)
    {
        $result = Cache::remember("rebate:" . $uid, 600, function () use ($uid) {
            return AssetLog::query()->where('uid', $uid)->where('type', ASSET_REBATE)->sum('amount');
        });

        return $result ?? 0;
    }


    public function history($params)
    {
        $where = [];

        $where[] = ['uid', $params['uid']];
        if (isset($params['type']) && $params['type'] > 0) {
            $where[] = ['type', "=", $params['type']];
        }

        $result = self::list(["*"], $where);
        foreach ($result['list'] as $k => $v) {

        }
        return $result;

    }
}
